package com.uy.cuantoconocesdeuruguay.animations;

import java.util.List;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;

import com.uy.cuantoconocesdeuruguay.MyChallenges;
import com.uy.cuantoconocesdeuruguay.adapters.MyChallengesViewAdapter;
import com.uy.cuantoconocesdeuruguay.controllers.GoogleAnalyticsTracker;
import com.uy.cuantoconocesdeuruguay.datatypes.FriendChallengeEnvelope;

@SuppressLint("NewApi")
public class HeightResizeAnimation extends Animation {

    private int startHeight;
    private int deltaHeight; // distance between start and end width
    private boolean isChallenge;
    private View view;
    private List<FriendChallengeEnvelope> challenges;
    private int position;
    private MyChallenges activity;
	private MyChallengesViewAdapter adapter;

    public HeightResizeAnimation (View v, boolean isChallenge) {
        this.view = v;
        this.isChallenge = isChallenge;
        super.setDuration(500);
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        view.getLayoutParams().height = (int) (startHeight + deltaHeight * interpolatedTime);
        view.requestLayout();
        if (isChallenge){
    		if (android.os.Build.VERSION.SDK_INT > 10) { 
    			view.setAlpha(1.0f - interpolatedTime);
    		}
	        if (view.getLayoutParams().height == 1){
	        	this.view.clearAnimation();
	        	new Handler().post(new Runnable() {
	                public void run() {
	                	try{
		                	adapter.remove(challenges.get(position));
		                	activity.setNewDataSet(challenges);
	                	}catch (Exception e){
	            			GoogleAnalyticsTracker.trackException(activity, e);
	            		}
	                }
	            });
	        }
        }else{
        	if (deltaHeight > 0){//Showing
        		if (android.os.Build.VERSION.SDK_INT > 10) { 
        			view.setAlpha(interpolatedTime);
        		}
        	}else{//Hiding
        		if (android.os.Build.VERSION.SDK_INT > 10) { 
        			view.setAlpha(1.0f - interpolatedTime);
        		}
        	}
        }
    }
    public void setParams(int startH, int endH) {
        this.startHeight = startH;
        deltaHeight = endH - startHeight;
    }
    @Override
    public void setDuration(long durationMillis) {
        super.setDuration(durationMillis);
    }

    @Override
    public boolean willChangeBounds() {
        return true;
    }

	public void setFinishParameters(MyChallenges activity, List<FriendChallengeEnvelope> challenges, int position, MyChallengesViewAdapter adapter) {
		this.activity = activity;
		this.challenges = challenges;
		this.position = position;
		this.adapter = adapter;
	}
}